package com.zzyl.nursing.util;

import com.zzyl.nursing.domain.NursingTask;
import org.springframework.stereotype.Component;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;

/**
 * 简化版护理任务生成工具类
 */
@Component
public class TaskGenerationUtil {

    private static final DateTimeFormatter TIME_FORMATTER = DateTimeFormatter.ofPattern("HH:mm");

    /**
     * 生成周期任务
     * @param baseTask 基础任务信息
     * @param startDate 开始日期
     * @param cycleType 周期类型：1-每日 2-每周 3-每月
     * @param frequency 执行频次
     * @param startTime 开始时间
     * @return 生成的任务列表
     */
    public static List<NursingTask> generateTasks(NursingTask baseTask, LocalDate startDate,
                                                  int cycleType, int frequency, String startTime) {
        List<NursingTask> tasks = new ArrayList<>();
        LocalTime start = LocalTime.parse(startTime, TIME_FORMATTER);

        // 根据周期类型选择日期计算函数
        Function<Integer, LocalDate> dateCalculator;
        switch (cycleType) {
            case 1: // 每日
                dateCalculator = i -> startDate;
                break;
            case 2: // 每周
                dateCalculator = i -> startDate.plusDays(7 / frequency * i);
                break;
            case 3: // 每月
                dateCalculator = i -> calculateMonthlyDate(startDate, frequency, i);
                break;
            default:
                throw new IllegalArgumentException("不支持的周期类型: " + cycleType);
        }

        // 根据周期类型选择时间计算函数
        Function<Integer, LocalTime> timeCalculator;
        if (cycleType == 1) {
            // 每日任务需要计算不同时间点
            int intervalMinutes = (24 * 60) / frequency;
            timeCalculator = i -> start.plusMinutes(intervalMinutes * i);
        } else {
            // 周/月任务使用相同时间点
            timeCalculator = i -> start;
        }

        // 生成任务
        for (int i = 0; i < frequency; i++) {
            NursingTask task = new NursingTask();
            copyBaseTaskInfo(baseTask, task);

            LocalDate taskDate = dateCalculator.apply(i);
            LocalTime taskTime = timeCalculator.apply(i);
            LocalDateTime estimatedTime = LocalDateTime.of(taskDate, taskTime);

            task.setEstimatedServerTime(estimatedTime);
        /*    task.setCycleType(cycleType);
            task.setExecutionFrequency(frequency);
            task.setStartTime(startTime);*/

            tasks.add(task);
        }

        return tasks;
    }

    /**
     * 计算月度任务日期
     */
    private static LocalDate calculateMonthlyDate(LocalDate startDate, int frequency, int index) {
        int interval = 30 / frequency;
        LocalDate taskDate = startDate.plusDays(interval * index);

        // 确保日期不超过当月最后一天
        int month = taskDate.getMonthValue();
        int year = taskDate.getYear();
        int lastDayOfMonth = taskDate.lengthOfMonth();

        if (taskDate.getDayOfMonth() > lastDayOfMonth) {
            taskDate = LocalDate.of(year, month, lastDayOfMonth);
        }

        return taskDate;
    }

    /**
     * 复制基础任务信息
     */
    private static void copyBaseTaskInfo(NursingTask source, NursingTask target) {
        // 使用BeanUtils复制所有字段
        com.zzyl.common.utils.bean.BeanUtils.copyProperties(source, target);
        // 重置ID，避免主键冲突
        target.setId(null);
        // 重置创建时间和更新时间
        target.setCreateTime(null);
        target.setUpdateTime(null);
    }
}